package com.briup.es.web.controller;

import com.briup.es.config.constraints.Add;
import com.briup.es.config.constraints.Update;
import com.briup.es.config.response.Result;
import com.briup.es.domain.bean.Address;
import com.briup.es.domain.vo.AddressVO;
import com.briup.es.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "用户收货地址管理模块")
@Validated
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @ApiOperation("获取所有的收获地址")
    @GetMapping
    public Result<?> queryAll() {
        List<AddressVO> addressVOS = addressService.queryAllByUserId();
        return Result.success(addressVOS)
                .apply("获取成功");
    }

    @ApiOperation("新增收货地址")
    @PostMapping
    public Result<?> addAddress(@Validated(Add.class) @RequestBody Address address) {
        addressService.addAddress(address);
        return Result.success();
    }

    @ApiOperation("修改收货地址")
    @PutMapping
    public Result<?> updateAddress(@Validated(Update.class) @RequestBody Address address) {
        addressService.updateAddress(address);
        return Result.success();
    }

    @ApiOperation("删除收货地址")
    @DeleteMapping
    public Result<?> deleteAddress(
            @NotNull(message = "地址id必须提供")
            @Min(value = 1, message = "地址id不能小于1") Integer id) {
        addressService.deleteAddress(id);
        return Result.success();
    }

    @ApiOperation("取消用户默认的收货地址")
    @PutMapping("/cancelDefault/{addressId}")
    public Result<?> cancelDefault(@PathVariable("addressId") Integer ignore) {
        addressService.updateDefault(null);
        return Result.success();
    }

    @ApiOperation("设置用户默认的收货地址")
    @PutMapping("/setDefault/{addressId}")
    public Result<?> setDefault(@PathVariable Integer addressId) {
        addressService.updateDefault(addressId);
        return Result.success();
    }
}
